package csc.traning.wpsj.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.traning.wpsj.dao.User;
import csc.traning.wpsj.dao.UserDB;

/**
 * Servlet implementation class UserRegister
 */
public class UserRegister extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserRegister() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		List<String> messages = new ArrayList<String>();

		if("confirm".equals(action)){
			String password = request.getParameter("password");
			String confirmPassword = request.getParameter("confirmPassword");
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String cellphone = request.getParameter("cellphone");
			String email = request.getParameter("email");
			String userName = request.getParameter("userName");

			User user = new User(userName, password, firstName, lastName,
					cellphone, email, "employee");
			
			request.getSession().setAttribute("userRegister", user);
			
			if (userName == null || "".equals(userName)) {
				messages.add("UserName is required field.");
			}
			if (firstName == null || "".equals(firstName)) {
				messages.add("Firstname is required field.");
			}
			if (lastName == null || "".equals(lastName)) {
				messages.add("Lastname is required field.");
			}
			if (email == null || "".equals(email)) {
				messages.add("Email is required field.");
			}
			if (password == null || "".equals(password)) {
				messages.add("Password is required field.");
			}
			if (confirmPassword == null || "".equals(confirmPassword)) {
				messages.add("Confirm Password is required field.");
			}
			
			if (password != null && confirmPassword != null) {
				if (!password.equals(confirmPassword)) {
					messages.add("The passwords doesn't match");
				}
			}
			
			
			if(messages.size() > 0){
				request.setAttribute("messages", messages);
				RequestDispatcher dispatcher = request.getRequestDispatcher("profile_register.jsp");
			    dispatcher.forward(request, response);
			}
			else{
				RequestDispatcher dispatcher = request.getRequestDispatcher("profile_register_confirm.jsp");
			    dispatcher.forward(request, response);
			}
		}
		else if ("add".equals(action)){
			HttpSession session = request.getSession();
			User userRegister = (User) session.getAttribute("userRegister");
			String message = null;
			if(userRegister != null){
				try {
					UserDB.addUser(userRegister);
					message = "Register successfully. Please use your account to log on.";
				} catch (Exception e1) {
					throw new ServletException(e1.getMessage());
				}
				session.removeAttribute("userRegister");
				
				request.setAttribute("message", message);
				RequestDispatcher dispatcher = request.getRequestDispatcher("logon.jsp");
			    dispatcher.forward(request, response);
			}else{
				throw new ServletException("User Not Found in Session scope any more.");
			}
			
		}
		
	}

}
